package com.atguigu.gmall.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.mapper.BaseCategoryViewMapper;
import com.atguigu.gmall.product.service.IndexService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 查询分类信息实现类
 */
@Service
public class IndexServiceImpl implements IndexService {
    @Resource
    private BaseCategoryViewMapper baseCategoryViewMapper;
    /**
     * 获得分类信息
     * @return
     */
    @Override
    public List<JSONObject> getIndexCategory() {
        //查询出所有一级二级三级分类的信息
        List<BaseCategoryView> categoryViewList = baseCategoryViewMapper.selectList(null);
        // 以一节分类为单位进行分桶
        Map<Long, List<BaseCategoryView>> category1Map
                = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
            //遍历一节分类的桶
        return category1Map.entrySet().stream().map(category1 ->{
            //初始化返回结果   存储所有一级分类
            JSONObject category1Json = new JSONObject();
            // 获取一节分类的Id
            Long category1Id = category1.getKey();
            category1Json.put("categoryId",category1Id);
            //获取map里的值 存储的是二级分类的信息和三级分类的信息
            List<BaseCategoryView> baseCategory2Views = category1.getValue();
            //获取每个一级分类的名字
            String categoryName = baseCategory2Views.get(0).getCategory1Name();
            category1Json.put("categoryName",categoryName);
            //对二级分类的信息进行分桶
            Map<Long, List<BaseCategoryView>> category2Map =
                    baseCategory2Views.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //遍历二级分类生成的桶
            List<JSONObject> category2JsonList = category2Map.entrySet().stream().map(category2 ->{
                //初始化返回结果 存储二接分类
                JSONObject category2Json = new JSONObject();
                //获取二级分类的Id
                Long category2Id = category2.getKey();
                category2Json.put("categoryId",category2Id);
                //获取三级分类的信息
                List<BaseCategoryView> baseCategory3Views = category2.getValue();
                //获取二级分类的名字
                String category2Name = baseCategory3Views.get(0).getCategory2Name();
                category2Json.put("categoryName",category2Name);
                //遍历每一个三级分类
                List<JSONObject> category3JsonList = baseCategory3Views.stream().map(category3 ->{
                    //返回结果初始化 存储三级分类
                    JSONObject category3Json = new JSONObject();
                    //获取三级分类的ID
                    Long category3Id = category3.getCategory3Id();
                    category3Json.put("categoryId",category3Id);
                    //获取三级分类的名字
                    String category3Name = category3.getCategory3Name();
                    category3Json.put("categoryName",category3Name);
                    //返回三级分类的map
                    return category3Json;
                }).collect(Collectors.toList());
                //将每个二级分类对应的三级分类保存
                category2Json.put("childCategory",category3JsonList);
                //返回二级分类
                return category2Json;
            }).collect(Collectors.toList());
            //将每个一级分类对应的二级分类保存
            category1Json.put("childCategory",category2JsonList);
            //返回一级分类
            return category1Json;
        }).collect(Collectors.toList());


    }
}
